<%@ Page language="vb" CodeFile="changeDriver.aspx.vb" Inherits="changeDriver_aspx_vb" %>

<%
    rowNumber = 1
    vehicleID = Request.QueryString("vehicleID")
    If Len(vehicleID) > 0 Then 
        vehicleID = CLng(vehicleID)
    Else
        Response.Redirect("searchDriver.aspx")
    End IF
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        dbOpen()
        RecSet = Conn.Execute("Select driverID From tblVehicle Where vehicleID = " & vehicleID & "")
        If RecSet.EOF Then 
            Response.Redirect("searchDriver.aspx")
        Else
            driverID = RecSet.Fields("driverID").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Over()
        strJS = ""
        strJS = strJS + "[""driverID"", ""<1""]"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>CHANGE DRIVER<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='changeDriver.aspx?do=save&vehicleID=" & vehicleID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Driver:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='driverID' id='driverID'>")
        Response.Write("<option value=''> - Select driver - ")
        dbOpen()
        RecSet = Conn.Execute("Select d.driverID, d.driver From (tblVehicle v INNER JOIN tblDriver d ON d.clientID = v.clientID) Where v.vehicleID = " & vehicleID & "")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                dbDriverID = RecSet.Fields("driverID").Value
                dbDriver = RecSet.Fields("driver").Value
                Response.Write("<option value='" & dbDriverID & "'")
                If CStr(driverID) = dbDriverID Then 
                    Response.Write(" selected='selected'")
                End IF
                Response.Write("> " & dbDriver & " ")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_driverID' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Save changes'></td></tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='2'>VEHICLE INFORMATION<hr class='hrHeadline'></td></tr>")
        dbOpen()
        RecSet = Conn.Execute("Select v.regnr, v.vinNumber, v.contractNumber, v.cylinderVolume, v.consumedDistance, v.discountPercent, v.completed, v.active, v.date, b.brand, m.model, u.username From (((tblVehicle v INNER JOIN tblBrand b ON b.brandID = v.brandID) INNER JOIN tblModel m ON m.modelID = v.modelID) INNER JOIN tblUser u ON u.userID = v.userID) Where v.vehicleID = " & vehicleID & "")
        If RecSet.EOF Then 
            Response.Write("<tr><td><font color='red'>ERROR</font></td></tr>")
        Else
            regnr = RecSet.Fields("regnr").Value
            vinNumber = RecSet.Fields("vinNumber").Value
            contractNumber = RecSet.Fields("contractNumber").Value
            cylinderVolume = RecSet.Fields("cylinderVolume").Value
            completed = RecSet.Fields("completed").Value
            active = RecSet.Fields("active").Value
            __date = RecSet.Fields("date").Value
            brand = RecSet.Fields("brand").Value
            model = RecSet.Fields("model").Value
            username = RecSet.Fields("username").Value
            If CInt(completed) = 0 Then 
                completed = "<font color='green'>No</font>"
            Else
                completed = "<font color='red'>Yes</font>"
            End IF
            If CInt(active) = 0 Then 
                active = "<font color='red'>No</red>"
            Else
                active = "<font color='green'>Yes</font>"
            End IF
            Response.Write("<tr bgcolor='#eeeeee'><td>Car make:</td><td align='right'>" & brand & " " & model & "</td></tr>")
            Response.Write("<tr><td>Licence number:</td><td align='right'>" & regnr & "</td></tr>")
            Response.Write("<tr bgcolor='#eeeeee'><td>VIN-number:</td><td align='right'>" & vinNumber & "</td></tr>")
            Response.Write("<tr><td>Contract number:</td><td align='right'>" & contractNumber & "</td></tr>")
            Response.Write("<tr bgcolor='#eeeeee'><td>Cylindervolume:</td><td align='right'>" & cylinderVolume & " cc</td></tr>")
            Response.Write("<tr><td>Lease contract active:</td><td align='right'>" & active & "</td></tr>")
            Response.Write("<tr bgcolor='#eeeeee'><td>Lease contract complete:</td><td align='right'>" & completed & "</td></tr>")
            Response.Write("<tr><td>Administrated by:</td><td align='right'>" & username & "</td></tr>")
            Response.Write("<tr bgcolor='#eeeeee'><td>First created:</td><td align='right'>" & Left(__date, 10) & "</td></tr>")
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        driverID = Request.Form("driverID")
        driverID = CLng(driverID)
        dbOpen()
        Conn.Execute("Update tblVehicle Set driverID = " & driverID & " Where vehicleID = " & vehicleID & "")
        dbClose()
        Response.Redirect("changeDriver.aspx?subDo=%20-%20SAVED&vehicleID=" & vehicleID & "")
    End IF
%>
